package net.kesumu.general.android.opengl.object;

import static net.kesumu.general.android.opengl.object.YushabuMessageConstants.*;
import static net.kesumu.general.android.opengl.util.Constants.BYTES_PER_FLOAT;

import java.util.List;

import net.kesumu.general.android.opengl.object.Geometry.Point;
import net.kesumu.general.android.opengl.object.Geometry.RingSector;
import net.kesumu.general.android.opengl.object.ObjectBuilder.DrawCommand;
import net.kesumu.general.android.opengl.object.ObjectBuilder.GeneratedData;
import net.kesumu.general.android.opengl.program.TextureShaderProgram;
import net.kesumu.general.android.opengl.util.VertexArray;

public class TextureRingSector {

    private static final int POSITION_COMPONENT_COUNT = 3;
    private static final int TEXTURE_COORDINATES_COMPONENT_COUNT = 2;
    private static final int STRIDE = (POSITION_COMPONENT_COUNT 
        + TEXTURE_COORDINATES_COMPONENT_COUNT) * BYTES_PER_FLOAT;
    
    public final int texture;
    
    private final VertexArray vertexArray;
    private final List<DrawCommand> drawList;
    
    public TextureRingSector(int texture, int numPoints) {
        this.texture = texture;
        
        float startAngle = 298f;
        float endAngle = 345f;
        
        RingSector fgRingSector = new RingSector(new Point(0f, 0f, 0f), 
                BASE_RADIUS - HISTORY_BAR_FG_WIDTH, 
                BASE_RADIUS , startAngle, endAngle);
    
        GeneratedData generatedData = ObjectBuilder.createYushabuMessageContent(fgRingSector, numPoints);

        vertexArray = new VertexArray(generatedData.vertexData);
        drawList = generatedData.drawList;
    }
    
    public void bindData(TextureShaderProgram textureProgram) {
        vertexArray.setVertexAttribPointer(
            0, 
            textureProgram.getPositionAttributeLocation(), 
            POSITION_COMPONENT_COUNT,
            STRIDE);
        
        vertexArray.setVertexAttribPointer(
            POSITION_COMPONENT_COUNT, 
            textureProgram.getTextureCoordinatesAttributeLocation(),
            TEXTURE_COORDINATES_COMPONENT_COUNT, 
            STRIDE);
    }
    
    public void draw() {
        for (DrawCommand drawCommand : drawList) {
            drawCommand.draw();
        }
    }

}
